package com.ykt.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.ykt.common.pay.wx.WxAppPayUtil;
import com.ykt.common.pay.wx.WxPayVo;
import com.ykt.model.Order;
import com.ykt.model.PayRecord;
import com.ykt.service.OrderService;
import com.ykt.service.PayRecordService;
import com.ykt.vo.ResultVo;

/**
 * APP调用微信支付操作
 *
 * @author wing
 * @version 1.0.0
 */
public class WeixinController extends BaseController {

    private final static Logger log = LoggerFactory.getLogger(WeixinController.class);

    private static Prop prop = PropKit.use("wx.properties");

    private PayRecordService payRecordService = enhance(PayRecordService.class.getName(), PayRecordService.class);
    private OrderService orderService = enhance(OrderService.class.getName(), OrderService.class);

    /**
     * 支付
     */
    public void pay() {
//        Integer orderId = getParaToInt("orderId", -1);
        String orderIds = getPara("orderIds", "0");
        Integer userId = getParaToInt("userId", -1);
        double amount = Double.valueOf(getPara("amount"));
        
        String[] orderArray = orderIds.split(",");
        Integer orderId = Integer.valueOf(orderArray[0]);
        
        /***************添加支付记录*******************************/
        String payNo = String.valueOf(System.currentTimeMillis());
        PayRecord pr = new PayRecord();
        pr.set("pay_no", payNo);
        pr.set("order_id", orderId);
        pr.set("user_id", userId);
        pr.set("account", amount);
        pr.set("pay_time", new Date());
        payRecordService.save(pr);
        /***************添加支付记录end*******************************/
        
        Order item = orderService.findById(orderId);
        
        //String wxOrderId = payNo;
        String wxOrderId = item.getStr("order_no");
        
        //int payId = pr.getId();
        log.info("----payNo==================" + payNo);
        String returnUrl = prop.get("wx.returnUrl");
        //微信支付
        WxPayVo tpWxPay = new WxPayVo();
        tpWxPay.setBody("金葡萄订单支付");
        //tpWxPay.setOrderId(String.valueOf(payId));
        tpWxPay.setOrderId(wxOrderId);
        String clientIp = getClientIpAddr();
        tpWxPay.setSpbillCreateIp(clientIp);
        tpWxPay.setTotalFee(String.valueOf(amount));
        tpWxPay.setNotifyUrl(returnUrl);
        log.info("----微信回调地址returnUrl==================" + returnUrl);

        Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
        result = WxAppPayUtil.getSignParam(tpWxPay);
        log.info("----返回app result====" + result);

        this.returnJson(ResultVo.success(result));
    }


    public String getClientIpAddr() {
        HttpServletRequest request = this.getRequest();

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        log.info("----请求IP----" + ip);
        return ip;
    }
}
